<template>
    <div class="login">
        <div class="loginbox">
            <h2>
                电商后台管理系统
            </h2>
            <!-- //表单 -->
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" class="demo-ruleForm">
                <el-form-item prop="username">
                    <el-input placeholder="请输入账号" prefix-icon="el-icon-user" v-model="ruleForm.username"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input placeholder="请输入密码" prefix-icon="el-icon-lock" v-model="ruleForm.password"
                        show-password></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
import { getlogin } from '.././utils/api'
export default {
    data() {
        return {
            ruleForm: {
                username: 'admin',
                password: '123456',
            },
            rules: {
                username: [
                    { required: true, message: '请输入账号', trigger: 'blur' },
                    { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 3, max: 6, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                ],
            }
        }
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    //登录
                    getlogin(this.ruleForm).then((res) => {
                        console.log(res);
                        if (res.data.meta.status == 200) {
                            window.localStorage.setItem('token', res.data.data.token);
                            this.$message({
                                message: res.data.meta.msg,
                                type: 'success'
                            });
                            this.$router.push('/home')
                        }else{
                            this.$message.error(res.data.meta.msg);
                        }
                    })
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        }
    },
    created() {

    },
    mounted() {

    },
    components: {

    },
    computed: {

    },
    watch: {

    },
}
</script>

<style lang='scss' scoped>
.login {
    box-sizing: border-box;
    padding-top: 200px;
}

.loginbox {
    background-color: #fff;
    width: 600px;
    height: 350px;
    box-sizing: border-box;
    padding: 20px;
    text-align: center;
    margin: auto;
}
</style>
